/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
{
    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);
    assert_equal(array3, ['a', 'b', 'c', 'd', 'e', 'f'])
}

{
    const letters = ["a", "b", "c"];
    const numbers = [1, 2, 3];

    const alphaNumeric = letters.concat(numbers);
    assert_equal(alphaNumeric, ['a', 'b', 'c', 1, 2, 3])
}

{
    const num1 = [1, 2, 3];
    const num2 = [4, 5, 6];
    const num3 = [7, 8, 9];

    const numbers1 = num1.concat(num2, num3);
    assert_equal(numbers1, [1, 2, 3, 4, 5, 6, 7, 8, 9])
}

{
    const letters1 = ["a", "b", "c"];
    const alphaNumeric1 = letters1.concat(1, [2, 3]);

    assert_equal(alphaNumeric1, ["a", "b", "c", 1, 2, 3])
}

{
    const num1 = [1, 2, 3];
    const num11 = [[1]];
    const num22 = [2, [3]];

    const numbers2 = num1.concat(num22);
    assert_equal(numbers2, [1, 2, 3, 2, [3]]);

    num11[0].push(4);
    assert_equal(numbers2, [1, 2, 3, 2, [3]]);
}

{
    assert_equal([1, , 3].concat([4, 5]), [1,, 3, 4, 5]);
    assert_equal([1, 2].concat([3, , 5]), [1, 2, 3,, 5]);
}

{
    const emptyArr = [];
    assert_equal(emptyArr.concat([]).length, 0)
}

/*
 * @tc.name:Array concat1
 * @tc.desc:test long Array Concat long Array and change proto
 * @tc.type: FUNC
 */
{
    let arr1 = new Array(2000);
    arr1[0] = 1;
    arr1[1] = 1;
    let arr2 = new Array(2000);
    arr2.__proto__ = arr1;
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(2000);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[2000], 1);
    assert_equal(arr4[2001], 3);
    assert_equal(arr4[2002], 2);
    assert_equal(arr4[2003], 'ab');
    assert_equal(arr4[2004], 1);
}

/*
 * @tc.name:Array concat2
 * @tc.desc:test long Array Concat short Array and change proto
 * @tc.type: FUNC
 */
{
    let arr1 = new Array(10);
    arr1[0] = 1;
    arr1[1] = 1;
    let arr2 = new Array(10);
    arr2.__proto__ = arr1;
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(2000);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[2000], 1);
    assert_equal(arr4[2001], 3);
    assert_equal(arr4[2002], 2);
    assert_equal(arr4[2003], 'ab');
    assert_equal(arr4[2004], 1);
}

/*
 * @tc.name:Array concat3
 * @tc.desc:test short Array Concat long Array and change proto
 * @tc.type: FUNC
 */
{
    let arr1 = new Array(2000);
    arr1[0] = 1;
    arr1[1] = 1;
    let arr2 = new Array(2000);
    arr2.__proto__ = arr1;
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(10);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[10], 1);
    assert_equal(arr4[11], 3);
    assert_equal(arr4[12], 2);
    assert_equal(arr4[13], 'ab');
    assert_equal(arr4[14], 1);
}

/*
 * @tc.name:Array concat4
 * @tc.desc:test long Array Concat long Array
 * @tc.type: FUNC
 */
{
    let arr2 = new Array(2000);
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(2000);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[2000], undefined);
    assert_equal(arr4[2001], 3);
    assert_equal(arr4[2002], 2);
    assert_equal(arr4[2003], 'ab');
    assert_equal(arr4[2004], 1);
}

/*
 * @tc.name:Array concat5
 * @tc.desc:test long Array Concat short Array
 * @tc.type: FUNC
 */
{
    let arr2 = new Array(10);
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(2000);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[2000], undefined);
    assert_equal(arr4[2001], 3);
    assert_equal(arr4[2002], 2);
    assert_equal(arr4[2003], 'ab');
    assert_equal(arr4[2004], 1);
}

/*
 * @tc.name:Array concat6
 * @tc.desc:test short Array Concat long Array
 * @tc.type: FUNC
 */
{
    let arr2 = new Array(2000);
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(10);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[10], undefined);
    assert_equal(arr4[11], 3);
    assert_equal(arr4[12], 2);
    assert_equal(arr4[13], 'ab');
    assert_equal(arr4[14], 1);
}

/*
 * @tc.name:Array concat7
 * @tc.desc:test long Array Concat long Array change origin prototype
 * @tc.type: FUNC
 */
{
    let arr2 = new Array(2000);
    arr2.__proto__[0] = "??";
    arr2[1] = 3;
    arr2[2] = 2;
    arr2[3] = "ab";
    arr2[4] = 1;
    let arr3 = new Array(2000);
    let arr4 = arr3.concat(arr2);

    assert_equal(arr4[0], '??');
    assert_equal(arr4[2000], '??');
    assert_equal(arr4[2001], 3);
    assert_equal(arr4[2002], 2);
    assert_equal(arr4[2003], 'ab');
    assert_equal(arr4[2004], 1);
}

{
    const v1 = [1, 2, "j", 9999, , 23];
    const v2 = ["sss", 2, "j", , , ,"test"];
    assert_equal(v1.concat(v2), [1, 2, "j", 9999, , 23, "sss", 2, "j", , , ,"test"]);
}

{
    let v3 = new Array();
    v3.push('foo');
    var v4 = ['bar'];
    var v5 = v3.concat(v4);
    assert_equal(v5, ['foo', 'bar']);
}

test_end();